package com.gardeneaters.game_v0.playingfield;

import com.google.gwt.canvas.client.Canvas;
import com.google.gwt.canvas.dom.client.Context2d;
import com.google.gwt.canvas.dom.client.CssColor;
import com.google.gwt.dom.client.CanvasElement;
import com.google.gwt.dom.client.ImageElement;
import com.google.gwt.event.dom.client.LoadEvent;
import com.google.gwt.event.dom.client.LoadHandler;
import com.google.gwt.user.client.Random;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.RootPanel;

public class Mushroom {
	
	 // private ImageElement image;
	  private CanvasElement mushroomImage;
	  private double power;
//	  private Image logoImg;
//	  private boolean imageLoaded;
	  private int actualHeight,actualWidth;
	  private double imgSize = 0.15;
	  private double HeightRatio = 0.1;
//	  private int dx,dy;
	  private double imgMaxSize;
//	  private String redMushroom;
	  private int x,y,width,height;
	  private Context2d context;
	  private double percentX,percentY,percentW,percentH;
	  
	  protected Mushroom(Context2d _context, double _percentX,double _percentY,double _percentW,double _percentH) {
		  this.context = _context;
		  this.percentX = _percentX;
		  this.percentY = _percentY;
		  this.percentW = _percentW;
		  this.percentH = _percentH; 
		  this.imgMaxSize = 1;
		  this.power = 0;
//		  this.mushroomImage = _mushroomImage.getCanvasElement();
//		  imageHeight = height;
//		  imageWidth = width;

	  }

	  protected void resize(int _x, int _y, int _width, int _height, Canvas _mushroomImage){
		  this.x = (int) (_x + percentX*_width);
		  this.y = (int) (_y + percentY*_height);
		  this.width = (int) (_width*percentW);
		  this.height = (int) (_height*percentH); 
		  actualHeight = _height;
		  actualWidth = _width;
		  this.mushroomImage = _mushroomImage.getCanvasElement();
	  }
	  
	  protected void draw(double _power) {

		  if(!inBufferRange(_power))
		  {
			  if(Double.compare(_power, power)>=0)
				  grow();
			  else
				  decompose();
		  }
		  context.save();
	      context.translate(x + width/2 - width*imgSize/2, y + height - height*HeightRatio);
	      context.scale(imgSize, HeightRatio);
	      context.drawImage(mushroomImage, 0, 0, width, height);
	      context.restore();
		  
	  }
	  
	  private void grow(){
		  if(Double.compare(1.0, power)==1)
		  {
			    if(imgSize<imgMaxSize)
			    {
				    if(imgSize<=imgMaxSize/2)
				    	imgSize = imgSize+ 0.0005*actualWidth;
				    else
				    	imgSize= imgSize+ 0.0003*actualWidth;
			    }
			    if(HeightRatio<imgMaxSize)
			    {
				    if(HeightRatio<imgMaxSize/2)
				    	HeightRatio = HeightRatio+ 0.0003*actualHeight;
				    else
				    	HeightRatio = HeightRatio+ 0.0005*actualHeight;
			    }
			    
			    
			    power = power + 0.0004*actualWidth;
		  }
	  }
	  
	  private void decompose(){
		  if(Double.compare(power,0.0)==1)
		  {
			    if(imgSize>0)
			    {
				    if(imgSize<=imgMaxSize/2)
				    	imgSize = imgSize- 0.0005*actualWidth;
				    else
				    	imgSize= imgSize- 0.0003*actualWidth;
			    }
			    if(HeightRatio>0)
			    {
				    if(HeightRatio<imgMaxSize/2)
				    	HeightRatio = HeightRatio- 0.0003*actualHeight;
				    else
				    	HeightRatio = HeightRatio- 0.0005*actualHeight;
			    }
			    
			    power = power - 0.0004*actualWidth;
		  }
	  }

	  protected void setImage(Canvas _mushroomImage) {
			
			this.mushroomImage = _mushroomImage.getCanvasElement();
	  }
	  
	  private boolean inBufferRange(double _power){
		  
		  if(Double.compare(0.0004*actualWidth,Math.abs(power-_power))>=0)
			  return true;
		  return false;
	  }
}
